package com.cqw.commontest.thread.unit2;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-06-04 15:30
 * @Description:
 */
public class Test3 {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建shoping
        Callable<String> callable = new Shopping();
        //使用FutureTask包装callable
        FutureTask<String> futureTask = new FutureTask<>(callable);
        //创建多线程
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println("线程启动了");
        //调用了get方法之后，线程会进入阻塞，知道获取到线程的执行结果为止
        String s = futureTask.get();
        System.out.println("获取到了执行结果");
        System.out.println("返回值：" + s);
    }
}


class Shopping implements Callable<String> {

    @Override
    public String call() throws Exception {
        //模拟空调送到家的时间
        System.out.println("空调配送中。。。");
        Thread.sleep(5000);
        System.out.println("空调送到家了。。。");
        return null;
    }
}
